/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.pps.web.bean.pupil;

import java.io.Serializable;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;
import javax.faces.context.FacesContext;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.collections.comparators.ReverseComparator;
import vn.com.pps.entities.Pupil;
import vn.com.pps.web.bean.course.CoursePupilBean;
import vn.com.pps.web.common.component.table.LazyCacheModel;
import vn.com.pps.web.ui.PupilTableItem;

/**
 *
 * @author Dai
 */
public class PupilManageBean extends LazyCacheModel implements Serializable {

    private int rowCount;
    private ArrayList<PupilTableItem> pupils;
    private String whereClause = "1=1";
    
    /*Search*/
    private String firstName = "";
    private String lastName = "";
    private String mobile = "";
    private boolean isView = true;
    private int selectedId;
    private Pupil pupil;

    public boolean isIsView() {
        return isView;
    }

    public int getRowCount() {
        return rowCount;
    }

    public ArrayList<PupilTableItem> getPupils() {
        return pupils;
    }

    public int getSelectedId() {
        return selectedId;
    }

    public void setSelectedId(int selectedId) {
        this.selectedId = selectedId;
    }

    public Pupil getPupil() {
        return pupil;
    }

    public void setPupil(Pupil pupil) {
        this.pupil = pupil;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    @Override
    protected void init() {

        rowCount = getServiceLocator().getPupilService().countPupil("1=1");
        setTargetPage(1);
        pupils = getServiceLocator().getPupilService().getPupils(getTargetPage(), getMaximumPageSize(), " 1=1");
        setPageItems(pupils);
        setRowCount(rowCount);

    }

    @Override
    public void sort(String pm_column, boolean pm_ascending) {
        Comparator<PupilTableItem> bc = null;
        if (pm_ascending) {
            bc = new BeanComparator(pm_column);
        } else {
            bc = new BeanComparator(pm_column, new ReverseComparator(new ComparableComparator()));
        }
        Collections.sort(getPageItems(), bc);
    }

    @Override
    public void loadData() {
        rowCount = getServiceLocator().getPupilService().countPupil(whereClause);
        pupils = getServiceLocator().getPupilService().getPupils(getTargetPage(), getMaximumPageSize(), whereClause);
        setPageItems(pupils);
        setRowCount(rowCount);
    }

    public String viewPupil() {
        isView = true;
        System.out.println("viewPupil ID:" + selectedId);
        pupil = getServiceLocator().getPupilService().getPupil(selectedId);
        FacesContext facesContext = FacesContext.getCurrentInstance();
        CoursePupilBean coursePupilBean
                = (CoursePupilBean) facesContext.getApplication()
                .createValueBinding("#{coursePupilBean}").getValue(facesContext);
        coursePupilBean.setPupilID(selectedId);
        coursePupilBean.loadData();
        return "success";

    }

    public String editViewPupil() {
        isView = false;
        System.out.println("viewPupil ID:" + selectedId);
        pupil = getServiceLocator().getPupilService().getPupil(selectedId);
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Pupil pupilEntity
                = (Pupil) facesContext.getApplication()
                .createValueBinding("#{pupilEntity}").getValue(facesContext);
        pupilEntity.setFirstName(pupil.getFirstName());
        pupilEntity.setLastName(pupil.getLastName());
        pupilEntity.setMobile(pupil.getMother());
        pupilEntity.setFather(pupil.getFather());
        pupilEntity.setMother(pupil.getMother());
        pupilEntity.setYearOfBirth(pupil.getYearOfBirth());
        pupilEntity.setNote(pupil.getNote());
        pupilEntity.setId(pupil.getId());

        return "success";

    }

    public void search() {
        whereClause = "1=1 ";
        if (firstName.trim().length() > 0) {
            whereClause += " AND FIRSTNAME LIKE '%" + firstName.trim() + "%'";
        }

        if (lastName.trim().length() > 0) {
            whereClause += " AND LASTNAME LIKE '%" + lastName.trim() + "%'";
        }

        if (mobile.trim().length() > 0) {
            whereClause += " AND MOBILE LIKE '%" + mobile.trim() + "%'";
        }

        System.out.println("Where::" + whereClause);
        rowCount = getServiceLocator().getPupilService().countPupil(whereClause);
        setTargetPage(1);
        pupils = getServiceLocator().getPupilService().getPupils(getTargetPage(), getMaximumPageSize(), whereClause);
        setPageItems(pupils);
        setRowCount(rowCount);
    }

    public void resetForm() {
        firstName = "";
        lastName = "";
        mobile = "";
    }
    
    public String addCourse(){
        return "seccess";
    }
    
    public String addPupil() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Pupil pupilEntity
                = (Pupil) facesContext.getApplication()
                .createValueBinding("#{pupilEntity}").getValue(facesContext);
        System.out.println("Pupil::" + pupilEntity.getLastName());
        Calendar calendar = Calendar.getInstance();
        java.sql.Timestamp currentTimeStamp = new java.sql.Timestamp(calendar.getTime().getTime());
        pupilEntity.setCreatedDate(currentTimeStamp);
        pupilEntity.setLastModifiedDate(currentTimeStamp);
        pupilEntity.setId(getServiceLocator().getPupilService().savePupil(pupilEntity));
        System.out.println("ssss:"+pupilEntity.getId());
        loadData();
        return "success";
    }
    public String preaddpupil(){
        return "success";
    }
    public String updatePupil() {

        FacesContext facesContext = FacesContext.getCurrentInstance();
        Pupil pupilEntity
                = (Pupil) facesContext.getApplication()
                .createValueBinding("#{pupilEntity}").getValue(facesContext);
        Calendar calendar = Calendar.getInstance();
        java.sql.Timestamp currentTimeStamp = new java.sql.Timestamp(calendar.getTime().getTime());
        pupilEntity.setLastModifiedDate(currentTimeStamp);
        getServiceLocator().getPupilService().editPupil(pupil);
        loadData();
        return "success";

    }
    
    

}
